//
//  ViewController.m
//  popaAnimation
//
//  Created by wanglei on 16/12/28.
//  Copyright © 2016年 wanglei. All rights reserved.
//

#import "ViewController.h"
#import <objc/objc-runtime.h>

@interface ViewController ()<UIGestureRecognizerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIViewController1 *vc1 = [[UIViewController1 alloc] init];
    // 最终会显示vc1
    [self setViewControllers:@[vc1] animated:YES];
    
    [self completeAnimation];
    
    // 原理分析
    //NSLog(@" interactivePopGestureRecognizer = %@",self.interactivePopGestureRecognizer);
    //
    //    unsigned int count = 0;
    //    // 获取UIGestureRecognizer的所有成员变量
    //    Ivar *ivar = class_copyIvarList([UIGestureRecognizer class], &count);
    //    for (int i = 0; i < count; i ++) {
    //        Ivar _var = *(ivar + i);
    //        NSLog(@"%s",ivar_getTypeEncoding(_var));
    //        NSLog(@"%s",ivar_getName(_var));
    //
    //    }
   // UIGestureRecognizer *gesture = self.interactivePopGestureRecognizer;
    //    NSMutableArray *_targets = [gesture valueForKey:@"_targets"];
    //    NSLog(@"%@",_targets);
    //    NSLog(@"&@",_targets[0]);
    //

    
}

- (void)completeAnimation{
    UIGestureRecognizer *gesture = self.interactivePopGestureRecognizer;
    gesture.enabled = NO;
    UIView *gestureView = gesture.view;
    UIPanGestureRecognizer *popRecognizer = [[UIPanGestureRecognizer alloc] init];
    popRecognizer.delegate = self;
    popRecognizer.maximumNumberOfTouches = 1;
   // self.popRecognizer = popRecognizer;
    [gestureView addGestureRecognizer:popRecognizer];
    
    /**
     *  获取系统手势的target数组
     */
    NSMutableArray *_targets = [gesture valueForKey:@"_targets"];
    /**
     *  获取它的唯一对象，我们知道它是一个叫UIGestureRecognizerTarget的私有类，它有一个属性叫_target
     */
    id gestureRecognizerTarget = [_targets firstObject];
    /**
     *  获取_target:_UINavigationInteractiveTransition，它有一个方法叫handleNavigationTransition:
     */
    id navigationInteractiveTransition = [gestureRecognizerTarget valueForKey:@"_target"];
    /**
     *  通过前面的打印，我们从控制台获取出来它的方法签名。
     */
    SEL handleTransition = NSSelectorFromString(@"handleNavigationTransition:");
    /**
     *  创建一个与系统一模一样的手势，我们只把它的类改为UIPanGestureRecognizer
     */
    [popRecognizer addTarget:navigationInteractiveTransition action:handleTransition];
    
}
// 实现代理方法
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    /**
     *  这里有两个条件不允许手势执行，1、当前控制器为根控制器；2、如果这个push、pop动画正在执行（私有属性）
     */
    return self.viewControllers.count != 1 && ![[self valueForKey:@"_isTransitioning"] boolValue];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
